package com.ruoyi.web.controller.library;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Catalog;
import com.ruoyi.system.domain.QzStation;
import com.ruoyi.system.mapper.QzStationMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author soulmate丶
 * @date 2021-08-02
 */
@RestController
@RequestMapping("/library/base")
public class LibraryController extends BaseController {

    @Autowired
    private QzStationMapper qzStationMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 获取数据列表
     * @param qzStation
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(QzStation qzStation){
        String userName = SecurityUtils.getUsername();
        System.out.println("用户名："+userName);
        SysUser sysUser = sysUserMapper.selectUserByUserName(userName);
        Long deptId = sysUser.getDeptId();
        List<QzStation> qzStations = new ArrayList<>();
        QueryWrapper<QzStation> queryWrapper = new QueryWrapper<>();
        if (qzStation.getTestName() != null && !qzStation.getTestName().isEmpty()){
            queryWrapper.like("test_name",qzStation.getTestName());
        }
        if (qzStation.getRemark() != null && !qzStation.getRemark().isEmpty()){
            queryWrapper.like("remark",qzStation.getRemark());
        }
        queryWrapper.ge("create_time",qzStation.getParams().get("beginTime") != null ? qzStation.getParams().get("beginTime") : "");
        queryWrapper.le("create_time",qzStation.getParams().get("endTime")+"23:59:59" != null ? qzStation.getParams().get("endTime")+" 23:59:59" : "");
        if (userName.equals("admin")){
            startPage();
            qzStations = qzStationMapper.selectList(queryWrapper);
        }else {
            startPage();
            queryWrapper.eq("dept_id",deptId);
            qzStations = qzStationMapper.selectList(queryWrapper);
        }
        return getDataTable(qzStations);
    }

    /**
     * 获取一条recode
     * @param stationId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('library:base:query')")
    @GetMapping(value = { "/", "/{stationId}" })
    public AjaxResult getInfo(@PathVariable(value = "stationId", required = false) String stationId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(stationId))
        {
            QueryWrapper<QzStation> queryWrapper = new QueryWrapper();
            queryWrapper.eq("id",stationId);
            ajax.put(AjaxResult.DATA_TAG, qzStationMapper.selectOne(queryWrapper));
        }
        return ajax;
    }

    /**
     * 新增终端
     */
    @PreAuthorize("@ss.hasPermi('library:base:add')")
    @Log(title = "干扰库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody QzStation qzStation)
    {
        String userName = SecurityUtils.getUsername();
        SysUser sysUser = sysUserMapper.selectUserByUserName(userName);
        Long deptId = sysUser.getDeptId();
        QueryWrapper<QzStation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sta_name",qzStation.getStaName());
        queryWrapper.eq("test_name",qzStation.getTestName());
        queryWrapper.like("create_time", DateUtils.getYesterday());
        if (qzStationMapper.selectList(queryWrapper).size()>0)
        {
            return AjaxResult.error("新增记录'" + qzStation.getStaName() + "'失败，记录已存在");
        }
        qzStation.setDeptId(String.valueOf(deptId));
        qzStation.setOperator(SecurityUtils.getUsername());
        qzStation.setCreateTime(DateUtils.getYesterday());
        return toAjax(qzStationMapper.insert(qzStation));
    }


    /**
     * 修改终端信息
     */
    @PreAuthorize("@ss.hasPermi('library:base:edit')")
    @Log(title = "干扰库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody QzStation qzStation)
    {
        qzStation.setOperator(SecurityUtils.getUsername());
        qzStation.setUpdateBy(SecurityUtils.getUsername());
        qzStation.setUpdateTime(DateUtils.getNowDate());
        return toAjax(qzStationMapper.updateById(qzStation));
    }

    /**
     * 删除终端
     */
    @PreAuthorize("@ss.hasPermi('library:base:remove')")
    @Log(title = "干扰库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{staIds}")
    public AjaxResult remove(@PathVariable String staIds)
    {
        return toAjax(qzStationMapper.deleteById(staIds));
    }

}
